<template>
	<view class="login_password_warp">
		<Header leftPathType="navigateTo" leftPath="/pages/reset_password/reset_password" :title="$t('common.reset.password')"/>
		<view class="login_password_content_warp">
			<view class="form_content_item">
				<view class="form_content_item_title">{{$t('mine.pwd.new')}}</view>
				<up-input
				    :placeholder="$t('mine.pwd.new.placeholder')"
				    border="surround"
				    v-model="form.newPassword"
					:customStyle="inputCss"
				  ></up-input>
			</view>
			<view class="form_content_item">
				<view class="form_content_item_title">{{$t('mine.pwd.new')}}</view>
				<up-input
				    :placeholder="$t('mine.pwd.new.placeholder')"
				    border="surround"
				    v-model="form.newPassword2"
					:customStyle="inputCss"
				  ></up-input>
			</view>
			
			<view class="but_comm_warp" @click="confirmFun">
				{{$t('common.confirm')}}
			</view>
		</view>
		<up-toast ref="uToastRef"></up-toast>
	</view>
</template>

<script>
	import Header from '@/components/header_comm2.vue';
	import CryptoJS from 'crypto-js';
	import { resetPassword } from '@/api/auth.js';
		
	export default {
		components:{Header},
		data() {
			return {
				form:{
					email:'',
					code:'',
					newPassword:'',
					newPassword2:'',
				},
				inputCss:{
					"height":'80rpx',
					"background": "#F3F3F3FF",
					"border-radius": "20rpx",
					"border": "0rpx",
					"padding-left": "16px",
				},
			};
		},
		onLoad(option) {
			const email = option.email;
			if(email && email !=="null"){
				this.form.email = email;
			}
			const code = option.code;
			if(code && code !=="null"){
				this.form.code = code;
			}
		},
		methods: {
			async confirmFun(){
				const form = this.form;
				if(!form.newPassword){
					this.$refs.uToastRef.show({
						type: 'error',
						message: this.$t('mine.pwd.new.placeholder'),
					});
					return;
				}
				if(!form.newPassword2){
					this.$refs.uToastRef.show({
						type: 'error',
						message: this.$t('mine.pwd.new2.placeholder'),
					});
					return;
				}
				if(form.newPassword.trim() !== form.newPassword2.trim()){
					this.$refs.uToastRef.show({
						type: 'error',
						message: this.$t('mine.pwd.different.placeholder'),
					});
					return;
				}
				const params={
					email:form.email,
					code:form.code,
					newPassword:CryptoJS.MD5(form.newPassword).toString(),
				}
				const response = await resetPassword(params);
				if(response && response.bizcode === 100){
					this.$refs.uToastRef.show({
						type: 'success',
						message: response.msg,
					});
					setTimeout(()=>{
						uni.redirectTo({
							url: '/pages/login/login'
						})
					},1500)
				}
			}
		}
	}
</script>

<style lang="scss">
.login_password_warp{
	height: calc(100vh - 0rpx);
	.login_password_content_warp{
		padding: 30rpx;
	}
	.but_comm_warp{
		margin-top: 100rpx;
	}
}
</style>
